//
//  ZYSelectVCModel.m
//  MerChant_car
//
//  Created by STAR on 2017/11/22.
//  Copyright © 2017年 陈越. All rights reserved.
//

#import "ZYSelectVCModel.h"
#import "ZYPresentationController.h"

@interface ZYSelectVCModel()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>
@property (nonatomic, assign) BOOL isPresent;
@property (nonatomic, strong) ZYPresentationController *presentC;
@end
@implementation ZYSelectVCModel


#pragma mark - UIViewControllerTransitioningDelegate
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source {
    _presentC = [[ZYPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
    _presentC.presentFrame = self.presentFrame;
    return _presentC;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    self.isPresent = YES;
    return self;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    self.isPresent = NO;
    return self;
}
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext {
    return 0.3;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    //弹出动画
    if (self.isPresent) {
        [self presentAnimateTransition:transitionContext];
        //消失动画
    }else {
        [self dismissAnimateTransition:transitionContext];
    }
}
- (void)presentAnimateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    [transitionContext.containerView addSubview:toView];
    toView.transform = CGAffineTransformMakeScale(1, 0.01);
    toView.layer.anchorPoint = CGPointMake(0.5, 0);
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        toView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}
- (void)dismissAnimateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.001 options:0 animations:^{
        fromView.transform = CGAffineTransformMakeScale(1, 0.01);
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
        [fromView removeFromSuperview];
    }];
}
@end
